<?php
/**
 * Kalaisoo
 *
 * A lightweight no-framework framework written in PHP
 *
 * @package Kalaisoo
 * @author $Author: info@sah-company.com $
 * @version $Id: test.php 129 2012-01-14 00:02:15Z info@sah-company.com $
 */

/**
 * Set error reporting mode to E_ALL | E_STRICT
 */
error_reporting(E_ALL | E_STRICT);

/**
 * Once you have finished your web application and everything works fine, set this constant
 * to something different than DEVELOPMENT. This will make sure that error management or
 * database stuff will not reveal developer information to the public. If you are using the
 * RedBean ORM library the database will freeze if you do not set this to development.
 *
 * @const string The modus in which our framework operates
 */
define('MODUS', 'DEVELOPMENT');

/**
 * @const string The default applications name
 */
define('APP_NAME', 'Kalaisoo');

/**
 * @const string The default applications slogan
 */
define('APP_SLOGAN', 'The no-framework framework written in PHP.');

/**
 * @const string The default stylesheet to use
 */
define('STYLESHEET_DEFAULT', 'test');

/**
 * @const string The system admin email address
 */
define('AUTHOR_EMAIL', 'info@example.com');

/**
 * @const int The default avatar size in pixels
 */
define('AVATAR_SIZE', 128);

/**
 * @const string The default meta description
 */
define('META_DESCRIPTION', 'This page was generated with Kalaisoo. Visit kalaisoo.com to learn about this no-framework framework');

/**
 * There is no need to change the following settings if you use Kalaisoo as it is.
 * Otherwise feel free to do so altogether. Have fun!
 */

/**
 * @const string The version string of our framework
 */
define('VERSION', 'Winter 2011');

/**
 * Path to your system folder.
 *
 * If you provide a leading / (slash) the system it is expected to be at that absolute path.
 *
 * @var string
 */
$sys = 'sys'; // e.g. '/Library/WebServer/Documents/sys' if you want to use another system

/**
 * Path to your application folder.
 *
 * If you provide a leading / (slash) the app it is expected to be at that absolute path.
 *
 * @var string
 */
$app = 'app'; // e.g. '/Library/WebServer/Documents/newsletter' to use the newsletter app

/**
 * The file extension under which php files will be looked up.
 *
 * @const string EXT
 */
define('EXT', '.php');

/**
 * @const string Base URL
 */
define('BASEURL', dirname($_SERVER['SCRIPT_NAME']));

/**
 * the document root of the filesystem
 *
 * @const string
 */
define('BASEPATH', $_SERVER['DOCUMENT_ROOT'].BASEURL);

/**
 * @const string SYS
 */
if (substr($sys, 0, 1) != '/') {
	$sys = BASEPATH.'/'.$sys;
}
define('SYS', $sys);

/**
 * @const string APP
 */
if (substr($app, 0, 1) != '/') {
	$app = BASEPATH.'/'.$app;
}
define('APP', $app);

/**
 * Load the configuration file.
 */
require_once APP.'/config/config'.EXT;

/**
 * Load the system.
 */
require_once SYS.'/core/refused'.EXT;

/**
 * Instanciate the Core class with our configuration array.
 */
$core = new Core($config);

/**
 * asrt
 *
 * @param mixed $a_given_value
 * @param mixed $the_expected_value
 * @return bool true | false
 */
function asrt($given, $expected)
{
	if ($given === $expected)
	{
		global $test_counter;
		$test_counter++;
		print " [{$test_counter}]";
		return true;
	}
	message("Failed test: Expected <code>{$expected}</code> but given <code>{$given}</code>");
	failed();
}

/**
 * failed
 *
 * Outputs a debug backtrace and message "FAILED" after a failed assertion
 * @see assert()
 */
function failed()
{
	message('FAILED');
	echo '<pre>';
	debug_print_backtrace();
	echo '</pre>';
	exit;
}

/**
 * passed
 *
 * Increments global $test_counter and prints it
 */
function passed()
{
	global $test_counter;
	$test_counter++;
	print " [{$test_counter}]";
}

/**
 * message
 *
 * @param string $msg
 */
function message($msg)
{
	echo "<br />".$msg;
}

/**
 * Write your tests here
 */

/**
 * Run the tests
 */
$test_counter = 0;
message('Testing<br />');

/**
 * PHP minimal version req. 5.2.11
 */
message('PHP Version');
asrt(version_compare(PHP_VERSION, '5.2.11', '<'), false);

/**
 * Addressbook testing
 */
$reg = Registry::instance();
// load redbean lib
$reg->loader->lib('redbean');

/**
 * Load our database configuration from the registry
 */
$config = Registry::instance()->config['dbTest'];

/**
 * Setup redbean with our test database parameters
 */
R::addDatabase('test', "{$config['driver']}:host={$config['host']};dbname={$config['database']}", $config['username'], $config['password']);

/**
 * use that test database for the test
 */
R::selectDatabase('test');

message('Nuke Database');
try {
	R::nuke();
	passed();
} catch (Exception $e) {
	failed();
}
// load the ab model
$reg->loader->model(array('addressbook', 'country'));
// dispense and setup some data
$ab = R::dispense('addressbook');
$person = R::dispense('person');
list($email1, $email2) = R::dispense('email', 2);
$phone = R::dispense('phone');
list($url1, $url2, $url3) = R::dispense('url', 3);
list($addr1, $addr2) = R::dispense('address', 2);
list($country_de, $country_us) = R::dispense('country', 2);
// slip in some data
$country_de->iso = 'de';
$country_de->name = 'Germany';
$country_us->iso = 'us';
$country_us->name = 'U.S. of A.';
$person->nickname = 'Klotzkopf';
$person->organization = 'ACME Corp.';
$person->maiden_name = 'Brown';
$person->suffix = 'Mr';
$person->home_page = 'www.comics.com';
$person->birth_date = '1920-01-01';
$person->title = '';
$person->department = 'Baseball';
$person->image = 'cbrown.png';
$person->note = 'This is of course the famous Charlie Brown';
$person->company = false;
$person->middle_name = 'M';
$person->job_title = 'Manager';
$person->last_name = 'Brown';
$person->first_name = 'Charlie';
// emails
$email1->label = 'Private';
$email1->value = 'private@example.com';
$email2->label = 'Office';
$email2->value = 'office@example.com';
// phone
$phone->label = 'Cell';
$phone->value = '+49 1234 777 444 67';
// urls
$url1->label = 'Private';
$url1->value = 'google.com';
$url2->label = 'Work';
$url2->value = 'apple.com';
$url3->label = 'Sparetime';
$url3->value = 'php.com';
// addresses
$addr1->city = 'New York';
$addr1->street = '7th Avenue';
$addr1->zip = '23444';
$addr1->country = $country_us;
$addr1->label = 'Work';
$addr1->state = 'NY';
$addr2->city = 'Frankfurt';
$addr2->street = 'Waldesweg 12';
$addr2->zip = '33334';
$addr2->country = $country_de;
$addr2->label = 'Private';
$addr2->state = 'Anywhere';
$ab->person = $person;
$ab->ownEmail = array($email1, $email2);
$ab->ownPhone = array($phone);
$ab->ownUrl = array($url1, $url2, $url3);
$ab->ownAddress = array($addr1, $addr2);

message('Addressbook');
try {
	R::store($ab);
	passed();
} catch (Exception $e) {
	echo $e;
	failed();
}
$id = $ab->getId();
asrt($id == 0, false);
$abShadow = R::load('addressbook', $id);
asrt($abShadow->person->nickname == 'Klotzkopf', true);
asrt(count($abShadow->ownUrl), 3);
foreach ($abShadow->ownUrl as $urlId=>$url) {
	unset($abShadow->ownUrl[$urlId]);
}
//sleep(1); // wait three seconds because of modification and creation time test
R::store($abShadow);
asrt(count($abShadow->ownUrl), 0);
// creation and modification date are no longer in addressbook

message('Aliasing and fetchAs');
list($owner, $lender) = R::dispense('people', 2);
$project = R::dispense('project');
$owner->name = 'Steve';
$lender->name = 'Alex';
R::store($owner);
R::store($lender);
$project->name = 'Book';
$project->owner = $owner;
$project->lender = $lender;
R::store($project);
$p = R::load('project', $project->getId());
$featureName = 'owner';
$aliasName = 'people';
$p_owner = $p->fetchAs($aliasName)->$featureName;
//$p_owner = $p->fetchAs('people')->owner;
$p_lender = $p->fetchAs('people')->lender;
asrt($project->fetchAs('people')->owner->getId() == $p_owner->getId(), true);
asrt($project->fetchAs('people')->owner->name == $p_owner->name, true);
asrt($project->fetchAs('people')->lender->getId() == $p_lender->getId(), true);
asrt($project->fetchAs('people')->lender->name == $p_lender->name, true);

//message('Gestalt');
//$reg->loader->lib('gestalt');
//$reg->loader->model('user');
//$bean = R::dispense('user');
//$g = new Gestalt($bean, 'shortinfo');
//asrt($g->output() == sprintf('I am the %s Gestalt of a %s', 'shortinfo', $bean->getMeta('type')), true);

/**
 * Thats all folks
 */
message('<br />All tests ran successfully. Bye!');
?>